package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageParam;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public PageResult<Dept> selectList(String name, Integer status, Integer page, Integer pageSize) {
        //第一步,进行分页调用第三方工具进项分页
        PageHelper.startPage(page, pageSize);
        //第二步,进行查询,调用mapper中的方法进行数据的查询
        List<Dept> depts = deptMapper.getList(name, status);
        //第三步,将查询结果进行封装成PageResult对象,并且将两次的查询结果进行合并
        Page<Dept> p = (Page<Dept>) depts;
        //返回给
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public List<Dept> allList() {
        List<Dept> depts=deptMapper.allList();
        return depts;
    }
}
